package com.technician.model.vo;

import cn.hutool.json.JSONUtil;
import com.technician.model.entity.Goods;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

@Data
public class GoodsVO implements Serializable {


    private static final long serialVersionUID = 3860717215783605669L;

    /**
     * 商品id
     */
    private Long id;

    /**
     * 商品名称
     */
    private String name;

    /**
     * 商品价格
     */
    private BigDecimal price;

    /**
     * 商品标签（json数组）
     */
    private List<String> tags;

    /**
     * 商品描述
     */
    private String details;

    /**
     * 商品图片（json数组）
     */
    private List<String> images;

    /**
     * 商品类型id
     */
    private Long type;

    /**
     * 包装类转对象
     *
     * @param goodsVO
     * @return
     */
    public static Goods voToObj(GoodsVO goodsVO) {
        if (goodsVO == null) {
            return null;
        }
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsVO, goods);
        List<String> tags = goodsVO.getTags();
        if (tags !=null){
            goods.setTags(JSONUtil.toJsonStr(tags));
        }
        List<String> images = goodsVO.getImages();
        if (images!=null){
            goods.setImages(JSONUtil.toJsonStr(images));
        }

        return goods;
    }

    /**
     * 对象转包装类
     *
     * @param goods
     * @return
     */
    public static GoodsVO objToVo(Goods goods) {
        if (goods == null) {
            return null;
        }
        GoodsVO goodsVO = new GoodsVO();
        BeanUtils.copyProperties(goods, goodsVO);
        List<String> tags = JSONUtil.toList(goods.getTags(), String.class);
        List<String> images = JSONUtil.toList(goods.getImages(), String.class);
        goodsVO.setTags(tags);
        goodsVO.setImages(images);
        return goodsVO;
    }
}
